/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年6月23日         Root.Lu
 */

package com.tianci.appstore.dataprovider;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.badsdk.SkyAppsClassifyProperties;
import com.skyworth.framework.skysdk.properties.SkyAppsProperties;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class LocalAppData {
    private Context mContext = null;
    private static final String STOREPACKAGE = "com.tianci.appstore";
    private static final String LAUNCHERPKG = "com.coocaa.launcher.EasyLauncherActivity";
    private List<String> appDatas = null;
    private HashMap<String, Boolean> mAppMap = null;
    private HashSet<String> mSysSet = new HashSet<String>();
    private List<String> mAppClassifyDatas = null;
    private List<String> mChildAppPkgs = null;
    private List<SkyListAppData> mChildApps = null;
    private HashSet<String> mClassifySet = new HashSet<String>();

    public LocalAppData(Context context) {
        mContext = context;
    }

    public List<String> getChildAppPkg() {
        getAppClassifyData();
        return mChildAppPkgs;
    }

    public List<SkyListAppData> getChildApps() {
        getAppClassifyData();
        mChildApps = new ArrayList<SkyListAppData>();
        if (mChildAppPkgs != null && mChildAppPkgs.size() > 0) {
            for (String pkg : mChildAppPkgs) {
                PackageManager manager = mContext.getPackageManager();
                try {
                    String name = manager.getApplicationLabel(
                            manager.getApplicationInfo(pkg, PackageManager.GET_META_DATA))
                            .toString();
                    SkyListAppData appData = new SkyListAppData(ListType.LOCAL_APP_LIST, "", name,
                            "", pkg);
                    LogUtils.d("luwei", "the child pkg name is " + pkg + " and name is " + name);
                    mChildApps.add(appData);
                } catch (NameNotFoundException e) {
                    e.printStackTrace();
                    continue;
                }
            }
        }
        return mChildApps;
    }

    private void getAppClassifyData() {
        if (mAppClassifyDatas == null) {
            mAppClassifyDatas = SkyAppsClassifyProperties.getPropertyList();
            if (mAppClassifyDatas != null && mAppClassifyDatas.size() > 0) {
                mChildAppPkgs = new ArrayList<String>();
                for (String pkg : mAppClassifyDatas) {
                    mClassifySet.add(pkg);
                    String prop = SkyAppsClassifyProperties.getProperty(pkg);
                    if (prop.contains("child")) {
                        mChildAppPkgs.add(pkg);
                    }
                }
            }
        }
    }

    public HashSet<String> getClassifyPkg() {
        getAppClassifyData();
        return mClassifySet;
    }

    private void getAppConfigData() {
        // mAppMap = AppStoreDB.getInstance().getAppConfigData();
        if (mAppMap != null && mAppMap.size() > 0) {
            return;
        } else {
            mAppMap = new HashMap<String, Boolean>();
        }
        if (appDatas == null) {
            appDatas = new ArrayList<String>();
            appDatas = SkyAppsProperties.getPropertyList();
            for (final String pkg : appDatas) {
                String prop = SkyAppsProperties.getProperty(pkg);
                if (prop.contains("myapp")) {
                    mAppMap.put(pkg, true);
                } else {
                    mAppMap.put(pkg, false);
                }
                if (prop.contains("sys")) {
                    mSysSet.add(pkg);
                }
            }
        }
    }

    public boolean isLowAppstore() {
        getAppConfigData();
        String prop = SkyAppsProperties.getProperty(STOREPACKAGE);
        if (prop != null)
            return prop.toLowerCase().contains("low");
        return false;
    }

    public HashSet<String> getSysPackage() {
        getAppConfigData();
        return mSysSet;
    }

}
